package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BStowageModelEnum;
import cn.huiyunche.base.service.interfaces.BStowageVehicleService;
import cn.huiyunche.base.service.mappers.BStowageModelMapper;
import cn.huiyunche.base.service.mappers.BStowageVehicleMapper;
import cn.huiyunche.base.service.model.BStowageModel;
import cn.huiyunche.base.service.model.BStowageModelExample;
import cn.huiyunche.base.service.model.BStowageVehicle;
import cn.huiyunche.base.service.model.BStowageVehicleExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class BStowageVehicleServiceImpl implements BStowageVehicleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BStowageVehicleServiceImpl.class);

    @Autowired
    private BStowageModelMapper bStowageModelMapper;

    @Autowired
    private BStowageVehicleMapper bStowageVehicleMapper;

    @Override
    public BStowageModel computeSuitableStowage(BStowageModelEnum modelEnum, List<Long> vehicleClassIds, List<Integer> vehicleNum) {
        LOGGER.info("prarms : {}, {}, {}", modelEnum, vehicleClassIds.size(), vehicleNum.size());

        List<BStowageVehicle> list = this.getListByVehicleClassIds(vehicleClassIds);
        if (CollectionUtils.isNotEmpty(list)) {
            int total = 0;
            for (int i = 0; i < list.size(); i++) {
                total += list.get(i).getStowageFactor().multiply(BigDecimal.valueOf(vehicleNum.get(i))).setScale(0, RoundingMode.UP).intValue();
            }

            BStowageModel model = this.getAvailable(modelEnum);
            if (!(total < model.getMax())) {
                return model;
            }
        }

        return null;
    }

    @Override
    public BStowageModel getAvailable(BStowageModelEnum modelEnum) {
        LOGGER.info("getAvailable param : {}", modelEnum.getValue());
        BStowageModelExample example = new BStowageModelExample();
        example.createCriteria().andStartTimeLessThanOrEqualTo(new Date()).andEndTimeGreaterThanOrEqualTo(new Date());
        List<BStowageModel> list = bStowageModelMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public List<BStowageVehicle> getListByVehicleClassIds(List<Long> vehicleClassIds) {
        LOGGER.info("getListByVehicleClassIds params : {}", vehicleClassIds);
        if (CollectionUtils.isEmpty(vehicleClassIds)) {
            LOGGER.error("vehicleClassIds is empty");
            throw new IllegalArgumentException("车型分类id不能为空");
        }

        BStowageVehicleExample example = new BStowageVehicleExample();
        example.createCriteria().andVehicleClassIdIn(vehicleClassIds);
        List<BStowageVehicle> list = bStowageVehicleMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list;
        }
        return null;
    }

    @Override
    public BStowageModel getById(Long id) {
        LOGGER.info("getById param : {}", id);
        if (null == id) {
            LOGGER.error("getById id must not be null");
            throw new IllegalArgumentException("配置模型id不能为空");
        }
        return bStowageModelMapper.selectByPrimaryKey(id);
    }

}
